package com.zidian.teacher.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zidian.teacher.R;
import com.zidian.teacher.utils.GetUserInfo;
import com.zidian.teacher.utils.ProgressBarBlackUtil;
import com.zidian.teacher.utils.ToastUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import com.zidian.teacher.adapter.ClassListAdapter;
import com.zidian.teacher.bean.ClassSelectBean;
import com.zidian.teacher.common.Constant;
import com.zidian.teacher.common.ToolBarActivity;

/**
 * Created by Administrator on 2016/9/18 0018.
 */
public class ClassListActivity extends ToolBarActivity {

    @Bind(R.id.list)
    RecyclerView mRecyclerView;
    TextView confrimBtn;

    private ClassListAdapter adapter;
    private List<Map<String, String>> data = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolbarView(R.layout.toolbar_both);
        setContentView(R.layout.activity_class_list);
        ButterKnife.bind(this);
        confrimBtn = (TextView) findViewById(R.id.toolbar_sure);
        setTitle("选择班级");
        initView();
        initData();

    }

    private void initView() {
        adapter = new ClassListAdapter(context, data);
        final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(adapter);


        //添加点击事件
        adapter.setOnItemClickListener(new ClassListAdapter.OnItemClickListener() {


            @Override
            public void onItemClick(View view, int position, ImageView imageView) {
                if ("1".equals(data.get(position).get("select"))){
                    data.get(position).remove("select");
                } else {
                    data.get(position).put("select", "1");
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });

        confrimBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String classText = "";
                for (int i = 0; i < data.size(); i++) {
                    if ("1".equals(data.get(i).get("select"))) {
                        if (classText.length() == 0) {
                            classText = data.get(i).get("className");
                        } else {
                            classText += "," + data.get(i).get("className");
                        }
                    }
                }

                Intent intent = new Intent();
                intent.putExtra("classText", classText);
                setResult(7001, intent);
                finish();


            }
        });


    }

    public void initData() {
        getData();
    }

    /**
     * 获取测试数据
     */
    private void getData() {
        String loginUrl = Constant.URL_TEACHER + "teacher/selectClassByTeacher";
        HashMap<String, String> map = new HashMap<>();
        map.put("teacherId", GetUserInfo.getTeacherId(context));
        map.put("token", GetUserInfo.getToken(context));

        ProgressBarBlackUtil.loadShow(context);

        OkHttpUtils.post().url(loginUrl).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtil.showToast(context, "获取失败！");
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e("T", response);
                ProgressBarBlackUtil.loadCancel();
                try {
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(response);
                    int resultCode = jsonObject.getInt("code");

                    if (resultCode != 200) {

                        ToastUtil.showToast(context, jsonObject.getString("message"));
                        return;
                    } else {
//                        ToastUtil.showToast(context, "成功！");
                    }

                } catch (JSONException e) {

                }
//
                Gson gson = new Gson();
                ClassSelectBean model = gson.fromJson(response, ClassSelectBean.class);
                if (model.isSuccess()) {
                    for (int i = 0; i < model.getDate().size(); i++) {
                        Map<String, String> map = new HashMap<>();
                        map.put("className", model.getDate().get(i));
                        map.put("select", "0");
                        data.add(map);
                    }

                    adapter.notifyDataSetChanged();
                    adapter.notifyItemRemoved(adapter.getItemCount());

                }

            }
        });


//        adapter.notifyDataSetChanged();
//        adapter.notifyItemRemoved(adapter.getItemCount());
    }
}
